package com.wyman.user_center_back.service.impl;
import java.sql.Timestamp;
import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wyman.user_center_back.constans.UserConstants;
import com.wyman.user_center_back.domain.User;
import com.wyman.user_center_back.mapper.UserMapper;
import com.wyman.user_center_back.service.UserService;
import com.wyman.user_center_back.utils.UserUtils;
import jakarta.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

/**
 * @author Wyman
 * @date 2024/8/31
 */

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService{

    @Autowired
    UserMapper userMapper;

    public Long userRegister(String userAccount,String userPassword,String checkedUserPassword){
//        校验 用户账号、用户密码、确认密码是否合规
        if(userAccount.length()<UserConstants.USER_ACCOUNT_MINIUM_LENGTH){
//          TODO:抛出错误 用户账户长度不合法
            return null;
        }
        if(userPassword.length()<UserConstants.USER_PASSWORD_MINIUM_LENGTH){
//          TODO:抛出错误 用户密码长度不合法
            return null;
        }
        if(!userPassword.equals(checkedUserPassword)){
//          TODO:抛出错误 两次输入密码不相等
            return null;
        }
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_account", userAccount);
        Long count = userMapper.selectCount(queryWrapper);
        if(count!=0){
//          TODO:抛出错误 用户名被抢注
            return null;
        }

        User user = new User();
        user.setUserAccount(userAccount);
        user.setUserPassword(DigestUtils.md5DigestAsHex(userPassword.getBytes()));
        user.setCreatetime(new Timestamp(new java.util.Date().getTime()));
        user.setUpdatetime(new Timestamp(new java.util.Date().getTime()));
        if(userMapper.insert(user)<=0){
//          TODO:抛出错误 插入操作失败
            return null;
        }
        return user.getId();
    }

    @Override
    public Boolean userLogin(String userAccount, String userPassword,HttpServletRequest request) {

        QueryWrapper queryWrapper = new QueryWrapper<User>();
        queryWrapper.eq("user_account",userAccount);
        User user = userMapper.selectOne(queryWrapper);
        if(user==null){
            //          TODO:抛出错误 用户名不存在
            return false;
        }
        String password = user.getUserPassword();
        if(!(DigestUtils.md5DigestAsHex(userPassword.getBytes()).equals(password))){
            //          TODO:抛出错误 用户密码错误
            return false;
        }
        User safeUser = UserUtils.getSecUser(user);
        request.getSession().setAttribute(UserConstants.USER_LOGIN_STATE,safeUser);
        return true;
    }

    @Override
    public User getCurrentUser(HttpServletRequest request) {
        User user = (User) request.getSession().getAttribute(UserConstants.USER_LOGIN_STATE);
        if(user==null){
            //          TODO:抛出错误 用户状态过期
            return null;
        }
        return UserUtils.getSecUser(user);
    }

    @Override
    public List<User> getAllUser() {
        return userMapper.selectList(null).stream().map(user->{
            return UserUtils.getSecUser(user);
        }).toList();
    }

    @Override
    public Boolean updateUser(User user) {

        long id = user.getId();
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        int i = userMapper.update(user,queryWrapper);

        return i>0?true:false;
    }

    @Override
    public Boolean deleteUser(long id) {
        int res = userMapper.deleteById(id);
        return res>0?true:false;
    }
}
